//package org.fat.nerd.actd.sharding.config;
//
//import com.baomidou.mybatisplus.core.MybatisConfiguration;
//import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
//import com.zaxxer.hikari.HikariDataSource;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.ibatis.session.SqlSessionFactory;
//import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
//import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
//import org.apache.shardingsphere.api.config.sharding.strategy.InlineShardingStrategyConfiguration;
//import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;
//import org.mybatis.spring.SqlSessionTemplate;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
//import org.springframework.jdbc.datasource.DataSourceTransactionManager;
//import org.springframework.transaction.PlatformTransactionManager;
//
//import javax.sql.DataSource;
//import java.sql.SQLException;
//import java.util.HashMap;
//import java.util.Map;
//import java.util.Properties;
//
///**
// * <p>description</p>
// *
// * @author TanXin
// * @since 2022/2/28 11:52
// */
//@Slf4j
//@Configuration
//public class ShardingJdbcConfig {
//
//    @Value("${spring.datasource.url}")
//    private String defaultJdbcUrl;
//    @Value("${spring.datasource.username}")
//    private String defaultUsername;
//    @Value("${spring.datasource.password}")
//    private String defaultPassword;
//    @Value("${spring.datasource.driver-class-name}")
//    private String defaultDriverClassName;
//
//
//    @Bean
//    @Qualifier("transactionManager")
//    public PlatformTransactionManager txManager(DataSource dataSource) {
//        return new DataSourceTransactionManager(dataSource);
//    }
//
//    @Bean(name = "defaultDataSource")
//    public DataSource getDefaultDataSource() {
//        HikariDataSource dataSource = new HikariDataSource();
//        dataSource.setJdbcUrl(defaultJdbcUrl);
//        dataSource.setUsername(defaultUsername);
//        dataSource.setPassword(defaultPassword);
//        dataSource.setDriverClassName(defaultDriverClassName);
//        dataSource.setMaximumPoolSize(100);
//        dataSource.setMinimumIdle(10);
//        dataSource.setConnectionTimeout(5000);
//        dataSource.setConnectionTestQuery("SELECT 1");
//        dataSource.setIdleTimeout(60 * 1000);
//        dataSource.setMaxLifetime(10 * 60 * 1000);
//        return dataSource;
//    }
//
//    @Bean
//    public DataSource dataSource() throws SQLException {
//        // 配置真实数据源
//        Map<String, DataSource> dataSourceMap = new HashMap<>();
//        //多数据源配置
//        //数据源1
//        dataSourceMap.put("db0", getDefaultDataSource());
//        // 配置分片规则
//        ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
//        //订单表分片规则
//        TableRuleConfiguration orderRuleConfig = new TableRuleConfiguration("t_order", "db0.t_order$->{0..3}");
//        //设置分表策略
//        orderRuleConfig.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("order_id", "t_order$->{order_id % 4}"));
//        shardingRuleConfig.getTableRuleConfigs().add(orderRuleConfig);
//        //可以继续用add添加分片规则
//        Properties otherProperties = new Properties();
//        //打印sql语句，生产环境关闭
//        otherProperties.setProperty("sql.show", Boolean.TRUE.toString());
//        return ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig, otherProperties);
//    }
//
//
//    @Bean
//    public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception {
//        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
//        bean.setDataSource(dataSource);
//        bean.setMapperLocations(new PathMatchingResourcePatternResolver()
//                .getResources("classpath*:mapper/*.xml"));
//        MybatisConfiguration configuration = new MybatisConfiguration();
//        configuration.setMapUnderscoreToCamelCase(true);
//        configuration.setLogImpl(org.apache.ibatis.logging.log4j2.Log4j2Impl.class);
//        configuration.setUseGeneratedKeys(true);
//        configuration.setUseColumnLabel(true);
//        bean.setConfiguration(configuration);
//        return bean.getObject();
//    }
//
//    @Bean
//    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
//        return new SqlSessionTemplate(sqlSessionFactory);
//    }
//
//
//}
